//
//  SYNewFeatureViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/18.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class SYNewFeatureViewController: UICollectionViewController {
    private let layout = UICollectionViewFlowLayout()
    
    init() {
        // 调用父类需要的layout的构造函数
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let itemCount = 4

    override func viewDidLoad() {
        super.viewDidLoad()

        setupPrePareUI()
    }
    
    func setupPrePareUI() {
        self.collectionView!.registerClass(SYNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
        
        
    }

    // MARK: UICollectionViewDataSource
    // #warning 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SYNewFeatureCell
    
        cell.index = indexPath.item
    
        return cell
    }
    
    //MARK: - 新特性第四张
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let newFeatureIndexPath = collectionView.indexPathsForVisibleItems().last!
//        print("newFeatureIndexPath\(newFeatureIndexPath)")
        
        if newFeatureIndexPath.item == itemCount - 1 {
        
            let cell = collectionView.cellForItemAtIndexPath(newFeatureIndexPath) as! SYNewFeatureCell
        
            cell.stratAnimation()
        }
    }


}

class SYNewFeatureCell: UICollectionViewCell {
    
    var index = 0 {
        didSet {
            
            backGroundView.image = UIImage(named: "new_feature_\(index + 1)")
            stratButton.hidden = true
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func stratAnimation() {
        stratButton.hidden = false
        
        stratButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.stratButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("动画完成")
        }
    
    
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareView()
        
    }
    
    func prepareView() {
        contentView.addSubview(backGroundView)
        contentView.addSubview(stratButton)
        
        
        backGroundView.ff_Fill(contentView)
        stratButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
/*
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
        stratButton.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

        contentView.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: stratButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: stratButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
*/
    }
    func startButtonClick() {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    
    }

    
    
    private lazy var backGroundView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    lazy var stratButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(imageLiteral: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
 
}
